/* global window */
import modelExtend from 'dva-model-extend'
import {message} from 'antd'
import {create, query, remove, update} from './services/highQualityFriendCircle'
import * as deptService from './services/highQualityFriendCircles'
import {pageModel} from 'utils/model'

export default modelExtend(pageModel, {
  namespace: 'highQualityFriendCircle',

  state: {
    currentItem: {},
    modalVisible: false,
    modalType: 'create',
    selectedRowKeys: [],
    empDataSource: [],
    activeKey: "group_list",
  },

  subscriptions: {
    setup ({dispatch, history}) {
      history.listen((location) => {
        if (location.pathname === '/highQualityFriendCircle') {
          const payload = location.query || {current: 1, size: 10}
          dispatch({
            type: 'query',
            payload,
          })
        }
      })
    },
  },

  effects: {

    * query ({payload = {}}, {call, put}) {
      const response = yield call(query, payload)
      const {data} = response
      if (response) {
        // setParent(data.records)
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.records,
          },
        })
      }
    },

    * delete ({payload}, {call, put, select}) {
      const data = yield call(remove, payload)
      const {selectedRowKeys} = yield select(_ => _.highQualityFriendCircle)
      if (data.code === 0) {
        yield put({type: 'updateState', payload: {selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload)}})
      } else {
        throw data
      }
    },

    * multiDelete ({payload}, {call, put}) {
      const data = yield call(deptService.remove, payload)
      if (data.code === 0) {
        yield put({type: 'updateState', payload: {selectedRowKeys: []}})
      } else {
        throw data
      }
    },

    * create ({payload}, {call, put}) {
      const data = yield call(create, payload)
      if (data.code === 0) {
        yield put({type: 'query'})
        message.success("优质朋友圈操作成功")
      } else {
        throw data
      }
    },
  },

  reducers: {

    showModal (state, {payload}) {
      return {...state, ...payload, modalVisible: true}
    },

    hideModal (state) {
      return {...state, modalVisible: false}
    },

  },
})
